package com.ubaer.zsycz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lsw.util.DensityUtil;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;
import com.lsw.view.ButtonTouch;
import com.ubaer.zsycz.R;

/**
 * Created by Administrator on 2018/9/25 0025.
 */

public class RechargeProcessingDialog {
    public Dialog alertDialog;
    protected Context context;
    View view;
    ButtonTouch btnOk, btnCancel;
    CheckBox cb_success, cb_fail;
    EditText et_content;
    LinearLayout ll_fail;
    TextView tv_order_no;
    TextView tv_chenggong, tv_shibai;


    public RechargeProcessingDialog(Context context) {
        this.context = context;
        init();
    }

    @SuppressLint("InflateParams")
    @SuppressWarnings("deprecation")
    private void init() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.dialog_recharge_processing, null);
        view.setMinimumWidth(DensityUtil.dip2px(context, 300));
        btnOk = view.findViewById(R.id.btnOk);
        btnCancel = view.findViewById(R.id.btnCancel);
        cb_success = view.findViewById(R.id.cb_success);
        cb_fail = view.findViewById(R.id.cb_fail);
        et_content = view.findViewById(R.id.et_content);
        ll_fail = view.findViewById(R.id.ll_fail);
        tv_order_no = view.findViewById(R.id.tv_order_no);
        tv_chenggong = view.findViewById(R.id.tv_chenggong);
        tv_shibai = view.findViewById(R.id.tv_shibai);
        alertDialog = new Dialog(context, R.style.dialogStyle);
        alertDialog.setContentView(view);
//        alertDialog.setCancelable(false);
        Window dialogWindow = alertDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_shibai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_fail.setChecked(true);
            }
        });
        tv_chenggong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_success.setChecked(true);
            }
        });
        cb_fail.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cb_success.setChecked(false);
                    ll_fail.setVisibility(View.VISIBLE);
                }
            }
        });
        cb_success.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cb_fail.setChecked(false);
                    ll_fail.setVisibility(View.GONE);
                }
            }
        });
    }

    public void show(String order_on, ProcessingResult processingResult) {
        tv_order_no.setText(order_on);
        cb_success.setChecked(true);
        cb_fail.setChecked(false);
        et_content.setText("");
        ll_fail.setVisibility(View.GONE);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (processingResult != null) {
                    String status = "0";
                    String reason = et_content.getText().toString().trim();
                    if (cb_fail.isChecked()) {
                        status = "2";
                        if (StringUtil.isNullOrEmpty(reason)) {
                            ToastUtil.showToast(context, "请填写失败原因！");
                            return;
                        }
                    } else if (cb_success.isChecked()) {
                        status = "1";
                    } else {
                        ToastUtil.showToast(context, "请选择充值状态！");
                        return;
                    }
                    processingResult.Result(status, reason);
                }
            }
        });

        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    public void dismiss() {
        if (alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }

    public interface ProcessingResult {
        void Result(String status, String reason);
    }
}
